package com.chinatelecom.epush.net;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 
 * HTTP请求封装
 */

public class ApacheHttpClient {

	private String response = null;

	// httppost请求
	public String httpPost(String url, List<NameValuePair> params)
			throws Exception {

		System.out.println("@@@@ url : " + url);
		if (params != null) {
			System.out.println("@@@@ request params : " + params.toString());
		}
		HttpClient httpclient = new DefaultHttpClient();
		// 请求超时
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
		// 读取信息超时
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				30000);
		HttpPost httppost = new HttpPost(url);
		try {
			if (params != null) {
				httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			}
			System.out.println("@@@@ response1 : " );
			HttpResponse httpResponse = httpclient.execute(httppost);
			System.out.println("@@@@ response4 : " );
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				response = EntityUtils.toString(httpResponse.getEntity());
			
			} else {
				response = "" + statusCode;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("@@@@ response : " + response);
		return response;
	}

	// httpget请求,带参数
	public String httpGetByParams(String url, List<NameValuePair> params)
			throws Exception {

		System.out.println("@@@@ url : " + url);
		if (params != null) {
			System.out.println("@@@@ request params : " + params.toString());
		}
		HttpClient httpclient = new DefaultHttpClient();
		// 请求超时
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
		// 读取信息超时
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				30000);
		try {
			if (params != null && params.size() != 0) {
				if (!url.contains("?")) {
					url += "?";
				} else {
					if (!url.endsWith("?") && !url.endsWith("&")) {
						url += "&";
					}
				}
				for (NameValuePair item : params) {
					url += item.getName() + "="
							+ URLEncoder.encode(item.getValue(), "UTF-8") + "&";
				}
				url = url.substring(0, url.length() - 1);
				System.out.println("@@@ get by params : " + url);
			}
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpclient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				response = EntityUtils.toString(httpResponse.getEntity());
			} else {
				response = "" + statusCode;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("@@@@ response : " + response);
		return response;
	}

	// httpget请求
	public String httpGet(String url) {
		HttpClient httpclient = new DefaultHttpClient();
		// 请求超时
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
		// 读取信息超时
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				30000);
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse;
		try {
			httpResponse = httpclient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				response = EntityUtils.toString(httpResponse.getEntity());
			} else {
				response = "" + statusCode;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	/**
	 * 获取目的地址图片资源
	 * 
	 * @param url
	 *            网络链接地址
	 * @return 网络图片对象
	 */
	public static Bitmap getNetBitmap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}